package com.koicarp.university.graduate.web.controller.information;

import com.koicarp.university.graduate.auth.annotation.OperLog;
import com.koicarp.university.graduate.auth.constant.ModuleNameEnum;
import com.koicarp.university.graduate.auth.constant.OperateLogTypeEnum;
import com.koicarp.university.graduate.service.entity.information.Information;
import com.koicarp.university.graduate.service.service.information.IInformationService;
import com.koicarp.university.graduate.service.vo.information.InformationVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 【资讯管理：资讯信息表】(Information)表控制层
 *
 * @author liutao
 * @since 2020-09-03 19:53:52
 */
@Slf4j
@RestController
@RequestMapping("information")
@RequiredArgsConstructor
public class InformationController {
    /**
     * 服务对象
     */

    private final IInformationService informationService;

    @PostMapping("getInformationWithPage")
    @ApiOperation(value = "资讯查询-普通查询，分页",notes = "资讯查询-普通查询，分页")
    public UniversityResponse getInformationWithPage(@RequestBody InformationVo vo){
        UniversityResponse response=null;
        try{
            if (vo.getSort().equals("+id")){
                vo.setSort("asc");
            }else{
                vo.setSort("desc");
            }
            response = informationService.getInformationWithPage(vo);
        }catch (Exception e){
            log.error("查询失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("saveInformation")
    @OperLog(moduleName = ModuleNameEnum.Information,type = OperateLogTypeEnum.CREATE,operDesc = "资讯信息增加(单个)")
    @ApiOperation(value = "资讯信息增加(单个)" ,notes = "资讯信息增加(单个)")
    public UniversityResponse saveInformation(@RequestBody Information information){
        UniversityResponse response=null;
        try{
            response = informationService.saveInformation(information);
        }catch (Exception e){
            log.error("增加失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
    @PostMapping("removeInformation")
    @ApiOperation(value = "删除资讯信息(单个)" ,notes = "删除资讯信息（单个）")
    @OperLog(moduleName = ModuleNameEnum.Information,type = OperateLogTypeEnum.DELETE,operDesc = "删除资讯信息(单个)")
    public UniversityResponse removeInformation(@RequestBody Information information){
        UniversityResponse response=null;
        try{
            response = informationService.deleteById(information.getId());
        }catch (Exception e){
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
    @PostMapping("removeListInfo")
    @ApiOperation(value = "删除资讯信息(多个)" ,notes = "删除资讯信息（多个）")
    @OperLog(moduleName = ModuleNameEnum.Information,type = OperateLogTypeEnum.DELETE,operDesc = "删除资讯信息（多个）")
    public UniversityResponse removeListInfo(@RequestBody List<Integer> idList){
        UniversityResponse response=null;
        try{
            response = informationService.deleteList(idList);
        }catch (Exception e){
            log.error("删除失败",e);
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }

    @PostMapping("modifyInformation")
    @ApiOperation(value = "修改资讯信息" ,notes = "修改资讯信息")
    @OperLog(moduleName = ModuleNameEnum.Information,type = OperateLogTypeEnum.UPDATE,operDesc = "修改资讯信息")
    public UniversityResponse modifyInformation(@RequestBody Information information){
        UniversityResponse response=null;
        try{
            response = informationService.update(information);
        }catch (Exception e){
            response = new UniversityResponse();
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
}